package xiyuan.localio;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class UidReader {
	private static Iterator<Long> iterator;
	public static boolean hasNext(){
		return iterator != null && iterator.hasNext();
	}
	public static Long next(){
		if (iterator != null && iterator.hasNext()){
			return iterator.next();
		}
		return -1L;
	}
	
	static{
		
		// 读取total_uid文件，获取一批可用的uid
		File file = new File("single_uid.txt");// 一次读一个先
		List<Long> uidlist = new ArrayList<Long>();
		if (file.exists()) {
			String s;
			BufferedReader br;
			try {
				br = new BufferedReader(new FileReader(file));
				while ((s = br.readLine()) != null) {
					System.out.println(s);
					uidlist.add(Long.parseLong(s)); // 加入文件中的uid
				}
				br.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (uidlist != null){
			iterator = uidlist.iterator();
		}
	}
}
